package inherit.practice.demo02.ArrayList04;

public class TestDelete {
    public static void main(String[] args) {
        Student[] arr = new Student[3];
        Student stu1 = new Student(1,"小平",18);
        Student stu2 = new Student(2,"小同",19);
        Student stu3 = new Student(3,"小神",20);
        arr[0] = stu1;
        arr[1] = stu2;
        arr[2] = stu3;
        // 通过id删除用户的，存在删除，不存在不删除
        int index = getIndex(arr,3);
        if(index >=0){
            arr[index] = null;
            printArr(arr);

        }else {
            System.out.println("id用户不存在");
        }
    }
    // 查找删除用户的id索引
    public static int getIndex(Student[] arr,int id){
        for (int i = 0; i < arr.length; i++) {
            Student stu = arr[i];
            if(stu != null){
                if(stu.getId() == id){
                    return i;
                }
            }
        }
        return -1;
    }
    public static void printArr(Student[] arr){
        for (int i = 0; i < arr.length; i++) {
            Student stu = arr[i];
            if(stu !=null){
                System.out.println(stu.getId()+","+stu.getName()+","+stu.getAge());
            }

        }
    }

}
